﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class RequestPerson : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strRequestID = Request.QueryString["RequestID"];
            string strPersonID = Request.QueryString["PersonID"];
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            int PersonID;
            if (!int.TryParse(strPersonID, out PersonID))
                Response.Redirect("~/");
            int RequestID;
            if (!int.TryParse(strRequestID, out RequestID))
                Response.Redirect("~/");
            DataAccessLayer.RequestPerson RequestPerson = DataAccessLayer.RequestPerson.FindByRequestIDPersonID_IncludeDeleted(db, RequestID, PersonID);
            if (RequestPerson == null)
                Response.Redirect("~/");
            FromDatabase(RequestPerson);
            btnDelete.Visible = true;
            lvHistory.DataSource = BusinessLogicLayer.DataObject.RequestPerson.Convert(DataAccessLayer.RequestPerson.HistoryToListByRequestIDPersonID(db, RequestID, PersonID));
            lvHistory.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strPersonID = Request.QueryString["PersonID"];
                string strRequestID = Request.QueryString["RequestID"];
                int PersonID = int.Parse(strPersonID);
                int RequestID = int.Parse(strRequestID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.RequestPerson RequestPerson = DataAccessLayer.RequestPerson.FindByRequestIDPersonID_IncludeDeleted(db, RequestID, PersonID);
                DataAccessLayer.RequestPerson NewRequestPerson = new DataAccessLayer.RequestPerson();
                NewRequestPerson.RequestID = RequestID;
                NewRequestPerson.PersonID = PersonID;
                NewRequestPerson.EditorUserID = UserID;
                NewRequestPerson.Timestamp = DateTime.Now;
                ToDatabase(NewRequestPerson);
                db.RequestPersons.InsertOnSubmit(NewRequestPerson);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRequestID = Request.QueryString["RequestID"];
                string strPersonID = Request.QueryString["PersonID"];
                int RequestID = int.Parse(strRequestID);
                int PersonID = int.Parse(strPersonID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.RequestPerson RequestPerson = DataAccessLayer.RequestPerson.FindByRequestIDPersonID_IncludeDeleted(db, RequestID, PersonID);
                DataAccessLayer.RequestPerson NewRequestPerson = new DataAccessLayer.RequestPerson();
                NewRequestPerson.RequestID = RequestID;
                NewRequestPerson.PersonID = PersonID;
                NewRequestPerson.EditorUserID = UserID;
                NewRequestPerson.Timestamp = DateTime.Now;
                NewRequestPerson.IsDeleted = true;
                ToDatabase(NewRequestPerson);
                db.RequestPersons.InsertOnSubmit(NewRequestPerson);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.RequestPerson RequestPerson)
        {
            ddlStatus.SelectedValue = RequestPerson.RequestPersonStatusID.ToString();
            lblDeletedItem.Visible = RequestPerson.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.RequestPerson RequestPerson)
        {
            RequestPerson.RequestPersonStatusID = int.Parse(ddlStatus.SelectedValue);
        }
    }
}